﻿/* Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 
Copyright (C) 2010 by Tomasz Pluskiewicz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

using System;
using Castle.Facilities.NHibernateIntegration;
using NHibernate;
using Yama.MVC.Dao.Connections;

namespace Yama.MVC.NHibernate.Connections
{
    ///<summary>
    /// Opens new sessions using the <see cref="ISessionManager"/> and connection name
    /// provided by an <see cref="IConnectionSelector"/> implementation
    ///</summary>
    public class NHibernateIntegrationSessionProvider : ISessionProvider
    {
        readonly ISessionManager manager;

        public IConnectionSelector ConnectionNameSelector { get; set; }

        /// <summary>
        /// Creates a new instance of <see cref="NHibernateIntegrationSessionProvider"/>
        /// </summary>
        public NHibernateIntegrationSessionProvider(ISessionManager manager)
        {
            this.manager = manager;
        }

        public virtual ISession Provide()
        {
            return Provide(null);
        }

        public virtual ISession Provide(string factoryName)
        {
            return factoryName == null ? manager.OpenSession() : manager.OpenSession(factoryName);
        }

        public virtual ISession CurrentSession
        {
            get
            {
                return ConnectionNameSelector == null ? Provide() : Provide(ConnectionNameSelector.CurrentConnectionName);
            }
        }
    }

    ///<summary>
    /// Opens new sessions using the <see cref="ISessionManager"/> and connection type
    /// provided by an <see cref="IConnectionSelector{T}"/> implementation
    ///</summary>
    public class NHibernateIntegrationSessionProvider<T> : NHibernateIntegrationSessionProvider, ISessionProvider<T>
    {
        public IConnectionSelector<T> ConnectionTypeSelector { get; set; }

        /// <summary>
        /// Creates a new instance of <see cref="NHibernateIntegrationSessionProvider{T}"/>
        /// </summary>
        public NHibernateIntegrationSessionProvider(ISessionManager manager) 
            : base(manager)
        {
        }

        public virtual ISession Provide(T dbConnectionType)
        {
            if (dbConnectionType.Equals(default(T)))
                return Provide(null);

            return base.Provide(Enum.GetName(typeof(T), dbConnectionType));
        }

        public override ISession CurrentSession
        {
            get
            {
                return ConnectionTypeSelector == null ? Provide() : Provide(ConnectionTypeSelector.CurrentConnectionType);
            }
        }
    }
}